﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule;

public partial class Inverntory_Target_Customer : PageBase
{

    private const string REQUEST_LIST_ID = "id";
    private int listID = 0;
    private const string RESPONSE_LIST_MAINTAIN_PAGE = "Inventory_Target_List.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[REQUEST_LIST_ID] != null)
        {
            int.TryParse(Request.QueryString[REQUEST_LIST_ID], out listID);
        }
        if (!IsPostBack)
        {
            GridPager1.GridView = GridView1;
            GridPager1.CurrentPage = PageIndex;
            this.InitForm();
            ddlSelected.SelectedValue = "1";
            BindGrid();
            ddlSelected.SelectedValue = "0";
        }
    }

    private void InitForm()
    {
        ICO_PROFILES list = new IcoProfileManager().Load(listID);
        if (list != null)
        {
            lblListName.Text = list.CODE;
        }

        ddlChain.DataTextField = "DESCRIPTION";
        ddlChain.DataValueField = "CODE";
        ddlChain.DataSource = new ChainManager().GetCourseTable(this.MyStateManager.DomainID);
        ddlChain.DataBind();
        ddlChain.Items.Insert(0,new ListItem("--所有--", ""));

        DictionaryItemsManager dim = new DictionaryItemsManager();
        ddlClass.DataTextField = "Name";
        ddlClass.DataValueField = "DictionaryItemId";
        ddlClass.DataSource = dim.GetItemList(DictionaryEnum.CustomerClass, this.MyStateManager.DomainID);
        ddlClass.DataBind();
        ddlClass.Items.Insert(0,new ListItem("--所有--", ""));

        ddlChannel.DataTextField = "Name";
        ddlChannel.DataValueField = "DictionaryItemId";
        ddlChannel.DataSource = dim.GetItemList(DictionaryEnum.CustomerChannel, this.MyStateManager.DomainID);
        ddlChannel.DataBind();
        ddlChannel.Items.Insert(0, new ListItem("--所有--", ""));
    }

    private void BindGrid()
    {
        if (listID > 0)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder(" AND C.DOMAIN_ID='" + this.MyStateManager.DomainID + "' ");

            if (ddlChain.SelectedValue != string.Empty)
            {
                sb.Append(" And CHAIN_CODE='"+ddlChain.SelectedValue+"' ");
            }
            if (ddlChannel.SelectedValue != string.Empty)
            {
                sb.Append(" AND LOCAL_LEVEL1_CODE =  '"+ddlChannel.SelectedValue +"'  ");
            }
            if (ddlClass.SelectedValue != string.Empty)
            {
                sb.Append(" AND CUST_CLASS_CODE =  '" + ddlClass.SelectedValue + "'  ");
            }
            if (txtCustomer.Text != string.Empty)
            {
                sb.Append(" AND C.[CODE] LIKE '%" + txtCustomer.Text.Trim() + "%' or C.[NAME] LIKE '%" + txtCustomer.Text.Trim() + "%' AND C.DOMAIN_ID='" + this.MyStateManager.DomainID + "' ");
            }
            DataSet ds = CustomerManager.SearchCustomerForIcoProfile(listID, sb.ToString(), ddlSelected.SelectedValue);
            if (ds != null && ds.Tables.Count > 0)
            {
                GridPager1.DataSource = ds.Tables[0];
                GridPager1.BindData();
            }
        }
    }

    protected void btnQuery_Click(object sender, EventArgs e)
    {
        BindGrid();
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        int customerID=0;

        if (listID > 0)
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                Control ctlLabelID = GridView1.Rows[i].FindControl("lblID");
                Control ctl = GridView1.Rows[i].FindControl("cbxSelected");
                CheckBox ckx = ctl as CheckBox;
                Label lblID = ctlLabelID as Label;
                int.TryParse(lblID.Text, out customerID);

                if (ckx != null && customerID > 0 && lblID!=null)
                {
                    CustomerManager.AssignCustomerForIcoProfile(ckx.Checked, customerID, this.listID);
                }
            }
        }
            Response.Redirect(RESPONSE_LIST_MAINTAIN_PAGE);
    }

    protected void gvChannelType_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Grid_RowDataBound(sender,e);
            DataRowView rowView = (DataRowView)e.Row.DataItem;

            bool selected = bool.Parse( rowView["Selected"].ToString());

            Control ctlCheckBox = e.Row.FindControl("cbxSelected");
            Control ctlLabelID = e.Row.FindControl("lblID");


            Label lblID = ctlLabelID as Label;
            Label lblProductListName=e.Row.FindControl("lblListName") as Label;
            CheckBox cbxSelected = ctlCheckBox as CheckBox;

            if (lblID != null)
            {
                lblID.Text = rowView["ID"].ToString();
            }
            
            if (cbxSelected != null)
            {
                if (lblProductListName != null)
                {
                    cbxSelected.Attributes.Add("onClick", "CheckList('" + cbxSelected.ClientID + "','" + lblProductListName.ClientID + "','" + lblListName.Text + "')");
                }
                if (selected)
                {
                    cbxSelected.Checked = true; ;
                }
                else
                {
                    cbxSelected.Checked = false;
                }
            }
        }
    }
}